<template>
  <div>
    <a-card :bordered="false" :bodyStyle="tstyle">
      <div class="table-page-search-wrapper" v-if="hasPerm('busPictureManager:page')">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="图片类型">
                <a-select style="width: 100%" v-model="queryParam.prictureType" placeholder="请选择图片类型">
                  <a-select-option v-for="(item,index) in prictureTypeData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="描述">
                <a-input v-model="queryParam.describe" allow-clear placeholder="请输入描述"/>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="创建时间">
                <a-date-picker style="width: 100%" placeholder="请选择创建时间" v-model="queryParam.createTimeDate" @change="onChangecreateTime"/>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24" >
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)" >查询</a-button>
                <a-button style="margin-left: 8px" @click="resetParam()">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false">
      <s-table
        ref="table"
        :columns="columns"
        :data="loadData"
        :alert="options.alert"
        :rowKey="(record) => record.id"
        :rowSelection="options.rowSelection"
      >
        <template class="table-operator" slot="operator" v-if="hasPerm('busPictureFabulous:add')" >
          <a-button type="danger" :disabled="selectedRowKeys.length < 1" v-if="hasPerm('busPictureFabulous:delete')" @click="batchDelete"><a-icon type="delete"/>批量删除</a-button>
        </template>


        <span slot="saveTime" slot-scope="text, record">
            {{ dataFormat(text) }}
          </span>
        <span slot="prictureTypeScopedSlots" slot-scope="text">
          {{ 'picture_type' | dictType(text) }}
        </span>
      </s-table>
    </a-card>
  </div>
</template>
<script>
  import { STable, XDown,Ellipsis } from '@/components'
  import moment from 'moment'
  import { busPictureManagerPage, busPictureManagerDelete, busPictureManagerExport } from '@/api/modular/main/buspicturemanager/busPictureManagerManage'
  export default {
    components: {
      STable,
      XDown,
      Ellipsis
    },
    data () {
      return {
        // 查询参数
        queryParam: {},
        // 表头
        columns: [
          {
            title: '图片名称',
            align: 'center',
            dataIndex: 'pictureName',
          },
          {
            title: '图片类型',
            align: 'center',
            dataIndex: 'prictureType',
            scopedSlots: { customRender: 'prictureTypeScopedSlots' }
          },
          {
            title: '点赞数',
            align: 'center',
            dataIndex: 'numberLikes',
          },
          {
            title: '创建者',
            align: 'center',
            dataIndex: 'createUserName',
          },

          {
            title: '图片描述',
            align: 'center',
            dataIndex: 'pictureDescribe'
          },
          {
            title: '保存时间',
            align: 'center',
            dataIndex: 'saveTime',
            scopedSlots: { customRender: 'saveTime' }
          },
        ],
        tstyle: { 'padding-bottom': '0px', 'margin-bottom': '10px' },
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return busPictureManagerPage(Object.assign(parameter, this.switchingDate())).then((res) => {
            return res.data
          })
        },
        prictureTypeData: [],
        selectedRowKeys: [],
        selectedRows: [],
        options: {
          alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
          rowSelection: {
            selectedRowKeys: this.selectedRowKeys,
            onChange: this.onSelectChange
          }
        }
      }
    },
    created () {
      const prictureTypeOption = this.$options
      this.prictureTypeData = prictureTypeOption.filters['dictData']('picture_type')
    },
    methods: {
      moment,
      /**
       * 查询参数组装
       */
      switchingDate () {
        const queryParamcreateTime = this.queryParam.createTimeDate
        if (queryParamcreateTime != null) {
            this.queryParam.createTime = moment(queryParamcreateTime).format('YYYY-MM-DD')
            if (queryParamcreateTime.length < 1) {
                delete this.queryParam.createTime
            }
        }
        const obj = JSON.parse(JSON.stringify(this.queryParam))
        return obj
      },
      /**
       * 单个删除
       */
      singleDelete (record) {
        const param = [{ 'id': record.id }]
        this.busPictureManagerDelete(param)
      },
      /**
       * 批量删除
       */
      batchDelete () {
        const paramIds = this.selectedRowKeys.map((d) => {
            return { 'id': d }
        })
        this.busPictureManagerDelete(paramIds)
      },
      dataFormat: function (datetime) {
        // console.log()
        return this.parseTime(datetime)
      },
      busPictureManagerDelete (record) {
        busPictureManagerDelete(record).then((res) => {
          if (res.success) {
            this.$message.success('删除成功')
            this.$refs.table.clearRefreshSelected()
          } else {
            this.$message.error('删除失败') // + res.message
          }
        })
      },
      onChangecreateTime(date, dateString) {
        this.createTimeDateString = dateString
      },

      handleOk () {
        this.$refs.table.refresh()
      },
      resetParam(){
        this.queryParam = {};
        this.$refs.table.refresh(true)
      },
      onSelectChange (selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      }
    }
  }
</script>
<style lang="less">
  .table-operator {
    margin-bottom: 18px;
  }
  button {
    margin-right: 8px;
  }
</style>
